<?php
// Include the current config file
require_once("../includes/config.php");

// Check to see if the script is already installed
if(isset($config['installed']))
{
	// Exit the script
	exit('PHPDugg is already installed.');
}

if( ini_get('safe_mode') )
{
	$safemode = 1;
}
else
{
	$safemode = 0;
}

if($safemode)
{
	if( (!file_exists('../images/thumbs')) OR (!file_exists('../images/avatars')) )
	{
		echo 'Your hosting is currently running in Safe Mode<br><Br>';
		echo 'You must manually create the following folders and chmod the directories to 0777<br><br>';
		echo 'images/<br>';
		echo 'images/thumbs/<br>';
		echo 'images/avatars<br><br><br>';
		echo 'Once you have done this <a href="index.php">click here</a> to reload this page';
		exit;
	}
}

if(!is_writable('../includes/config.php'))
{
		echo 'Your config.php file isn\'t currently writtable<br><Br>';
		echo 'You must set the permisions on this file to 0777<br><br>';
		echo 'Once you have done this <a href="index.php">click here</a> to reload this page';
		exit;
}

// If they have submitted the form
if(isset($_POST['DBName']))
{
	// Check that their config file is writtable
	if(is_writable('../includes/config.php'))
	{
		// Check that their thumbs folder is writtable
		if(is_writable('../images/'))
		{
			if(!file_exists('../images/thumbs'))
			{
				mkdir('../images/thumbs/');
			}
			
			if(!file_exists('../images/avatars'))
			{
				mkdir('../images/avatars/');
			}
		
			// Try to connect to the database details they provided
			if(mysql_connect($_POST['DBHost'], $_POST['DBUser'], $_POST['DBPass']))
			{
				// Try to select the database specified
				if(mysql_select_db($_POST['DBName']))
				{
					$site_path = str_replace('\\','/',ereg_replace('install', '', dirname(__FILE__)));
					$site_url = "http://" . $_SERVER['HTTP_HOST'] . ereg_replace ("index.php", "", ereg_replace ("install/", "", $_SERVER['PHP_SELF']));
							
					// Content that will be written to the config file
					$content = "<?php\n";
					$content.= "\$config['db']['host'] = '".addslashes($_POST['DBHost'])."';\n";
					$content.= "\$config['db']['name'] = '".addslashes($_POST['DBName'])."';\n";
					$content.= "\$config['db']['user'] = '".addslashes($_POST['DBUser'])."';\n";
					$content.= "\$config['db']['pass'] = '".addslashes($_POST['DBPass'])."';\n";
					$content.= "\$config['db']['pre'] = '".addslashes($_POST['DBPre'])."';\n";
					$content.= "\n";
					$content.= "\$config['site_title'] = 'PHPDugg';\n";
					$content.= "\$config['site_url'] = '".addslashes($site_url)."';\n";
					$content.= "\$config['site_path'] = '".addslashes($site_path)."';\n";
					$content.= "\$config['admin_email'] = '".addslashes(stripslashes($config['admin_email']))."';\n";
					$content.= "\$config['upcomtime'] = '".$config['upcomtime']."';\n";
					$content.= "\$config['usedigg'] = '".$config['usedigg']."';\n";
					$content.= "\$config['vidthumb'] = '".$config['vidthumb']."';\n";
					$content.= "\$config['mod_rewrite'] = '".addslashes(stripslashes($config['mod_rewrite']))."';\n";
					$content.= "\$config['anounce_en'] = '".addslashes(stripslashes($config['anounce_en']))."';\n";
					$content.= "\$config['anounce_txt'] = '".addslashes(stripslashes($config['anounce_txt']))."';\n";
					$content.= "\$config['safe'] = '".$safemode."';\n";
					$content.= "\n";
					$content.= "\$config['cookie_time'] = '".addslashes(stripslashes($config['cookie_time']))."';\n";
					$content.= "\$config['cookie_name'] = '".addslashes(stripslashes($config['cookie_name']))."';\n";
					$content.= "\n";
					$content.= "\$config['email']['type'] = '".addslashes(stripslashes($config['email']['type']))."';\n";
					$content.= "\$config['email']['smtp']['host'] = '".addslashes(stripslashes($config['email']['smtp']['host']))."';\n";
					$content.= "\$config['email']['smtp']['user'] = '".addslashes(stripslashes($config['email']['smtp']['user']))."';\n";
					$content.= "\$config['email']['smtp']['pass'] = '".addslashes(stripslashes($config['email']['smtp']['pass']))."';\n";
					$content.= "\n";
					$content.= "\$config['tpl_name'] = '".$config['tpl_name']."';\n";
					$content.= "\$config['version'] = '".$config['version']."';\n";
					$content.= "\$config['lang'] = '".addslashes(stripslashes($config['lang']))."';\n";
					$content.= "\$config['installed'] = '1';\n";
					$content.= "?>";
				
					// Open the includes/config.php for writting
					$handle = fopen('../includes/config.php', 'w');
					// Write the config file
					fwrite($handle, $content);
					// Close the file
					fclose($handle);
				
					// Create Admin Table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."admins` (  `admin_id` int(11) unsigned NOT NULL auto_increment,  `username` varchar(40) NOT NULL default '',  `password` varchar(40) NOT NULL default '',  PRIMARY KEY  (`admin_id`)) TYPE=MyISAM;");
					// Insert Admin Details			
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."admins` VALUES (1, '".addslashes($_POST['adminuser'])."', '".addslashes(md5($_POST['adminpass']))."');");
					// Create Admin Menu table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."amenu` (  `menu_id` smallint(3) unsigned NOT NULL auto_increment,  `sort_id` smallint(3) unsigned NOT NULL default '0',  `parent_id` smallint(3) unsigned NOT NULL default '0',  `menu_title` varchar(40) NOT NULL default '',  `menu_icon` varchar(100) NOT NULL default '',  `menu_url` varchar(255) NOT NULL default '',  `menu_target` varchar(10) NOT NULL default '',  `menu_desc` varchar(100) NOT NULL default '',  PRIMARY KEY  (`menu_id`)) TYPE=MyISAM;");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (38, 0, 0, 'Configuration', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (39, 0, 38, 'Site Details', '<img src=\"images/icons/smicon_config.gif\">', 'site_details.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (40, 10, 38, 'Database', '<img src=\"images/icons/smicon_config.gif\">', 'database.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (46, 20, 38, 'Digg.com Mode', '<img src=\"images/icons/smicon_digglogo.gif\">', 'usedigg.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (41, 20, 0, 'Users', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (42, 0, 41, 'Search Users', '<img src=\"images/icons/smicon_search.gif\">', 'search_users.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (43, 10, 0, 'Categories', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (44, 0, 43, 'Add Category', '<img src=\"images/icons/smicon_addrule.gif\">', 'category_add.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (45, 2, 43, 'Edit Categories', '<img src=\"images/icons/smicon_editrule.gif\">', 'category_view.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (47, 30, 0, 'Stories', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (48, 0, 47, 'Search Stories', '<img src=\"images/icons/smicon_search.gif\">', 'search_stories.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (83, 40, 0, 'Admins', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (84, 0, 83, 'Add Admin', '<img src=\"images/icons/smicon_addrule.gif\">', 'admin_add.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (85, 0, 83, 'Edit Admin', '<img src=\"images/icons/smicon_editrule.gif\">', 'admin_view.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (86, 50, 0, 'Logout', '', 'logout.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (87, 35, 0, 'Content', '', '', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (88, 10, 87, 'Add Content', '<img src=\"images/icons/smicon_addrule.gif\">', 'content_add.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (89, 20, 87, 'Edit Content', '<img src=\"images/icons/smicon_editrule.gif\">', 'content_edit.php', '', '');");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."amenu` VALUES (90, 10, 38, 'Templates', '<img src=\"images/icons/smicon_template.gif\">', 'template_settings.php', '', '');");
					// Create HTML table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."html` (  `html_id` varchar(8) NOT NULL default '',  `template` tinyint(1) unsigned NOT NULL default '0',  `html_title` varchar(255) NOT NULL default '',  `html_content` text NOT NULL,  PRIMARY KEY  (`html_id`)) ENGINE=MyISAM");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."html` VALUES ('terms', 0, 'Terms and Conditions', 'Your Terms and Conditions go here, to edit this page login to your admin panel and go to Edit Content.');");
					// Create Category Table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."cats` (  `cat_id` int(11) unsigned NOT NULL auto_increment,  `parent_id` int(11) unsigned NOT NULL default '0',  `cat_title` varchar(50) NOT NULL default '',  `cat_stories` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`cat_id`)) TYPE=MyISAM;");
					// Insert Default Categories
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (1, 0, 'Technology', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (2, 0, 'Science', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (3, 0, 'World & Business', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (4, 0, 'Sports', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (5, 0, 'Videos', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (6, 0, 'Entertainment', 0);");
					mysql_query("INSERT INTO `".addslashes($_POST['DBPre'])."cats` VALUES (7, 0, 'Gaming', 0);");
					// Create Comments Table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."comm` (  `comment_id` int(11) unsigned NOT NULL auto_increment,  `story_id` int(11) unsigned NOT NULL default '0',  `user_id` int(11) unsigned NOT NULL default '0',  `user_name` varchar(50) NOT NULL default '',  `comment_desc` mediumtext NOT NULL,  `comment_time` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`comment_id`),  KEY `story_id` (`story_id`)) TYPE=MyISAM;");
					// Create Cronjob table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."cron` (  `story_id` int(11) unsigned NOT NULL default '0',  `digs` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`story_id`)) TYPE=MyISAM;");
					// Create Digs table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."digs` (  `story_id` int(11) unsigned NOT NULL default '0',  `user_id` int(11) unsigned NOT NULL default '0',  `time` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`story_id`,`user_id`)) TYPE=MyISAM;");
					// Create Stories Table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."stories` (  `story_id` int(11) unsigned NOT NULL auto_increment,  `user_id` int(11) unsigned NOT NULL default '0',  `user_name` varchar(50) NOT NULL default '',  `story_url` varchar(255) NOT NULL default '',  `story_title` varchar(80) NOT NULL default '',  `story_desc` mediumtext NOT NULL,  `story_cat` int(11) unsigned NOT NULL default '0',  `story_rating` int(11) unsigned NOT NULL default '0',  `story_digs` mediumint(8) unsigned NOT NULL default '0',  `story_buries` mediumint(8) unsigned NOT NULL default '0',  `story_time` int(11) unsigned NOT NULL default '0',  `story_comments` mediumint(8) unsigned NOT NULL default '0',  `story_last5` int(11) unsigned NOT NULL default '0',  `story_thumb` varchar(80) NOT NULL default '',  PRIMARY KEY  (`story_id`)) TYPE=MyISAM;");
					// Create Users Table			
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."users` (  `user_id` int(11) unsigned NOT NULL auto_increment,  `username` varchar(50) NOT NULL default '',  `password` varchar(50) NOT NULL default '',  `email` varchar(255) NOT NULL default '',  `forgot` varchar(40) NOT NULL default '',  `remember` varchar(40) NOT NULL default '', `avatar` tinyint(1) unsigned NOT NULL default '0',  PRIMARY KEY  (`user_id`)) TYPE=MyISAM;");
					// Create Buries Table
					mysql_query("CREATE TABLE `".addslashes($_POST['DBPre'])."buries` (`story_id` int(11) unsigned NOT NULL default '0',  `user_id` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`story_id`,`user_id`)) TYPE=MyISAM;");
					
					header("Location: cron.php");
					exit;
				}
				else
				{
					// The database doesn't exist
					echo '<script>alert(\'Could not find database '.$_POST['DBName'].'\r\n\r\n'.addslashes(mysql_error()).'\');</script>';
				}
			}
			else
			{
				// The username/password does not have access to that database
				echo '<script>alert(\'Could not connect to MySQL using these details.\r\n\r\n'.addslashes(mysql_error()).'\');</script>';
			}
		}
		else
		{
			// Can't write to the thumbs folder
			echo '<script>alert(\'Could not write to your images folder.\r\n\r\nPlease check that you have set the chmod/permisions to 0777\');</script>';
		}
	}
	else
	{
		// Can't write to the includes/config.php file
		echo '<script>alert(\'Could not write to your includes/config.php file.\r\n\r\nPlease check that you have set the chmod/permisions to 0777\');</script>';
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHPDugg - Install</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
}
.style12 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
.style15 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; }
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
-->
</style>
</head>

<body>
<div align="center">
 <br>
  <form name="form1" method="post" action="index.php">
  <table border="0" cellpadding="0" cellspacing="0">
    <tr><td>  Welcome to the PHPDugg installation, Please fill in the details<br>
  below and then click &quot;Install&quot;<br>
  <br>
  <Br>
    <table border="0" cellspacing="0" cellpadding="3">
      <tr>
        <td colspan="3"><span class="style15">MySQL Details</span></td>
      </tr>
      <tr>
        <td><span class="style12">MySQL Host: </span></td>
        <td><input style="width:150px;" name="DBHost" type="text" id="DBHost" value="<? if(isset($_POST['DBHost'])){ echo $_POST['DBHost']; } ELSE { echo 'localhost'; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The host address of your MySQL database, usually localhost');">(?)</a> </span></td>
      </tr>
      <tr>
        <td><span class="style12">MySQL Username:</span></td>
        <td><input style="width:150px;" name="DBUser" type="text" id="DBUser" value="<? if(isset($_POST['DBUser'])){ echo $_POST['DBUser']; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The username used to access your MySQL database');">(?)</a> </span></td>
      </tr>
      <tr>
        <td><span class="style12">MySQL Password:</span></td>
        <td><input style="width:150px;" name="DBPass" type="password" id="DBPass" value="<? if(isset($_POST['DBPass'])){ echo $_POST['DBPass']; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The password used to access your MySQL database');">(?)</a> </span></td>
      </tr>
      <tr>
        <td><span class="style12">MySQL Database Name: </span></td>
        <td><input style="width:150px;" name="DBName" type="text" id="DBName" value="<? if(isset($_POST['DBName'])){ echo $_POST['DBName']; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The name of your database, you must have first created this.');">(?)</a> </span></td>
      </tr>
      <tr>
        <td><span class="style12">MySQL Table Prefix: </span></td>
        <td><input style="width:150px;" name="DBPre" type="text" id="DBPre" value="<? if(isset($_POST['DBPre'])){ echo $_POST['DBPre']; } else { echo 'dugg_'; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The text that follows the table names, not required');">(?)</a> </span></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="3"><span class="style15">Admin Details </span></td>
      </tr>
      <tr>
        <td><span class="style12">Admin Username: </span></td>
        <td><input style="width:150px;" name="adminuser" type="text" id="adminuser" value="<? if(isset($_POST['adminuser'])){ echo $_POST['adminuser']; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The username that will be used when managing the site');">(?)</a> </span></td>
      </tr>
      <tr>
        <td><span class="style12">Admin Password: </span></td>
        <td><input style="width:150px;" name="adminpass" type="password" id="adminpass" value="<? if(isset($_POST['adminpass'])){ echo $_POST['adminpass']; } ?>"></td>
        <td><span class="style12">&nbsp;<a href="javascript:alert('The password that will be used when managing the site');">(?)</a> </span></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input style="width:150px;" name="Submit" type="submit" value="Install"></td>
        <td>&nbsp;</td>
      </tr>
    </table>
	</td></tr></table>
  </form>
  <span class="footer style1">  <br>
  <br>
  &copy; 2006 <a href="http://www.kubelabs.com/phpdugg.php">Kubelabs.com</a></span>
</div>
</body>
</html>